Skip to main content

bool

>>> dir(bool)
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']

Definition

A Boolean value (bool) is a logical value: True or False. It answers questions like “Is this statement correct?” For instance, 1 == 2 is False.

bool is a subclass of int, so in many circumstances booleans are evaulated like numbers.

Using booleans

If a statement is true then it will evaluate to True. Conversely, if a statement is false then it will evaluate to False.

>>> 1 == 1
True

>>> 1 == 2
False

>>> "Hello" == "World"
False

>>> "Hello" == "Hello"
True

Types with falsy values

The boolean value of an empty type will always be False.

TypeExampleNotes
boolFalseThe literal False itself
NoneTypeNoneRepresents “no value”
int, float, complex0, 0.0, 0jAny numeric zero
str""Empty string
list[]Empty list
tuple()Empty tuple
dict{}Empty dictionary
setset()Empty set
frozensetfrozenset()Empty frozen set
rangerange(0)Empty range object

Likewise a custom class that ony returns 0 or False will be falsy.

Types with truthy values

The boolean value of a non-empty type will always be True.

TypeExampleNotes
boolTrueThe literal True
int, float, complex1, 3.14, -2+0jAny nonzero
str"Hello"Any non-empty string
list[1, 2, 3]Any non-empty list
tuple(1, 2)Any non-empty tuple
dict{"a": 1}Any non-empty dictionary
set{"x", "y"}Any non-empty set
frozensetfrozenset({1})Any non-empty frozen set
rangerange(1, 5)Any range with at least one value

Checking for truthiness

The boolean value of a type can be checked by using the bool() function.

>>> bool("")
False

>>> bool(0)
False

>>> bool("99 problems but a bool aint one")
True

Operations: Truth tables

The and operator

ExpressionResultExplanation
True and TrueTrueBoth values are true
True and FalseFalseOne is false
False and TrueFalseOne is false
False and FalseFalseBoth are false

The or operator

ExpressionResultExplanation
True or TrueTrueAt least one is true
True or FalseTrueFirst value is true
False or TrueTrueSecond value is true
False or FalseFalseBoth are false

The not operator

ExpressionResultExplanation
not TrueFalseNegates the value
not FalseTrueNegates the value

Dunder methods

Dunder MethodOperationExample (normal syntax)Example (dunder call)
__bool__Truth valuebool(True)TrueTrue.__bool__()
__and__Logical AND / Bitwise &True & FalseFalseTrue.__and__(False)
__or__Logical OR / Bitwise |True | FalseTrueTrue.__or__(False)
__xor__Logical XOR / Bitwise ^True ^ FalseTrueTrue.__xor__(False)
__invert__Bitwise NOT~True-2True.__invert__()
__eq__Equality comparisonTrue == FalseFalseTrue.__eq__(False)
__ne__Inequality comparisonTrue != FalseTrueTrue.__ne__(False)
__lt__Less thanFalse < TrueTrueFalse.__lt__(True)
__le__Less than or equalTrue <= TrueTrueTrue.__le__(True)
__gt__Greater thanTrue > FalseTrueTrue.__gt__(False)
__ge__Greater than or equalTrue >= FalseTrueTrue.__ge__(False)
__add__AdditionTrue + True2True.__add__(True)
__sub__SubtractionTrue - False1True.__sub__(False)
__mul__MultiplicationTrue * 55True.__mul__(5)
__int__Convert to integerint(True)1True.__int__()
__float__Convert to floatfloat(False)0.0False.__float__()
__index__Integer index value[10, 20][True]20True.__index__()
__str__String conversionstr(True)'True'True.__str__()
__repr__Representationrepr(True)'True'True.__repr__()
__hash__Hash valuehash(True)1True.__hash__()

Boolean methods

All boolean methods are inherited from int.